1 // -*- C++ -*- C forwarding header.
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file include/cmath
28 * This is a Standard C++ Library file. You should @c \#include this file
29 * in your programs, rather than any of the @a *.h implementation files.
31 * This is the C++ version of the Standard C Library header @c math.h,
32 * and its contents are (mostly) the same as that header, but are all
33 * contained in the namespace @c std (except for names which are defined
38 // ISO C++ 14882: 26.5 C library
41 #pragma GCC system_header
43 #include <bits/c++config.h>
44 #include <bits/cpp_type_traits.h>
45 #include <ext/type_traits.h>
48 #ifndef _GLIBCXX_CMATH
49 #define _GLIBCXX_CMATH 1
51 // Get rid of those macros defined in <math.h> in lieu of real functions.
77 _GLIBCXX_BEGIN_NAMESPACE(std)
79 // Forward declaration of a helper function. This really should be
80 // an `exported' forward declaration.
81 template<typename _Tp>
82 _Tp __cmath_power(_Tp, unsigned int);
84 template<typename _Tp>
86 __pow_helper(_Tp __x, int __n)
89 ? _Tp(1)/__cmath_power(__x, -__n)
90 : __cmath_power(__x, __n);
95 { return __builtin_fabs(__x); }
99 { return __builtin_fabsf(__x); }
103 { return __builtin_fabsl(__x); }
105 template<typename _Tp>
106 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
109 { return __builtin_fabs(__x); }
115 { return __builtin_acosf(__x); }
118 acos(long double __x)
119 { return __builtin_acosl(__x); }
121 template<typename _Tp>
122 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
125 { return __builtin_acos(__x); }
131 { return __builtin_asinf(__x); }
134 asin(long double __x)
135 { return __builtin_asinl(__x); }
137 template<typename _Tp>
138 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
141 { return __builtin_asin(__x); }
147 { return __builtin_atanf(__x); }
150 atan(long double __x)
151 { return __builtin_atanl(__x); }
153 template<typename _Tp>
154 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
157 { return __builtin_atan(__x); }
162 atan2(float __y, float __x)
163 { return __builtin_atan2f(__y, __x); }
166 atan2(long double __y, long double __x)
167 { return __builtin_atan2l(__y, __x); }
169 template<typename _Tp, typename _Up>
171 typename __gnu_cxx::__promote_2<
172 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
173 && __is_arithmetic<_Up>::__value,
174 _Tp>::__type, _Up>::__type
175 atan2(_Tp __y, _Up __x)
177 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
178 return atan2(__type(__y), __type(__x));
185 { return __builtin_ceilf(__x); }
188 ceil(long double __x)
189 { return __builtin_ceill(__x); }
191 template<typename _Tp>
192 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
195 { return __builtin_ceil(__x); }
201 { return __builtin_cosf(__x); }
205 { return __builtin_cosl(__x); }
207 template<typename _Tp>
208 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
211 { return __builtin_cos(__x); }
217 { return __builtin_coshf(__x); }
220 cosh(long double __x)
221 { return __builtin_coshl(__x); }
223 template<typename _Tp>
224 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
227 { return __builtin_cosh(__x); }
233 { return __builtin_expf(__x); }
237 { return __builtin_expl(__x); }
239 template<typename _Tp>
240 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
243 { return __builtin_exp(__x); }
249 { return __builtin_fabsf(__x); }
252 fabs(long double __x)
253 { return __builtin_fabsl(__x); }
255 template<typename _Tp>
256 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
259 { return __builtin_fabs(__x); }
265 { return __builtin_floorf(__x); }
268 floor(long double __x)
269 { return __builtin_floorl(__x); }
271 template<typename _Tp>
272 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
275 { return __builtin_floor(__x); }
280 fmod(float __x, float __y)
281 { return __builtin_fmodf(__x, __y); }
284 fmod(long double __x, long double __y)
285 { return __builtin_fmodl(__x, __y); }
290 frexp(float __x, int* __exp)
291 { return __builtin_frexpf(__x, __exp); }
294 frexp(long double __x, int* __exp)
295 { return __builtin_frexpl(__x, __exp); }
297 template<typename _Tp>
298 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
300 frexp(_Tp __x, int* __exp)
301 { return __builtin_frexp(__x, __exp); }
306 ldexp(float __x, int __exp)
307 { return __builtin_ldexpf(__x, __exp); }
310 ldexp(long double __x, int __exp)
311 { return __builtin_ldexpl(__x, __exp); }
313 template<typename _Tp>
314 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
316 ldexp(_Tp __x, int __exp)
317 { return __builtin_ldexp(__x, __exp); }
323 { return __builtin_logf(__x); }
327 { return __builtin_logl(__x); }
329 template<typename _Tp>
330 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
333 { return __builtin_log(__x); }
339 { return __builtin_log10f(__x); }
342 log10(long double __x)
343 { return __builtin_log10l(__x); }
345 template<typename _Tp>
346 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
349 { return __builtin_log10(__x); }
354 modf(float __x, float* __iptr)
355 { return __builtin_modff(__x, __iptr); }
358 modf(long double __x, long double* __iptr)
359 { return __builtin_modfl(__x, __iptr); }
364 pow(float __x, float __y)
365 { return __builtin_powf(__x, __y); }
368 pow(long double __x, long double __y)
369 { return __builtin_powl(__x, __y); }
371 #ifndef __GXX_EXPERIMENTAL_CXX0X__
372 // _GLIBCXX_RESOLVE_LIB_DEFECTS
373 // DR 550. What should the return type of pow(float,int) be?
375 pow(double __x, int __i)
376 { return __builtin_powi(__x, __i); }
379 pow(float __x, int __n)
380 { return __builtin_powif(__x, __n); }
383 pow(long double __x, int __n)
384 { return __builtin_powil(__x, __n); }
387 template<typename _Tp, typename _Up>
389 typename __gnu_cxx::__promote_2<
390 typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value
391 && __is_arithmetic<_Up>::__value,
392 _Tp>::__type, _Up>::__type
393 pow(_Tp __x, _Up __y)
395 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
396 return pow(__type(__x), __type(__y));
403 { return __builtin_sinf(__x); }
407 { return __builtin_sinl(__x); }
409 template<typename _Tp>
410 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
413 { return __builtin_sin(__x); }
419 { return __builtin_sinhf(__x); }
422 sinh(long double __x)
423 { return __builtin_sinhl(__x); }
425 template<typename _Tp>
426 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
429 { return __builtin_sinh(__x); }
435 { return __builtin_sqrtf(__x); }
438 sqrt(long double __x)
439 { return __builtin_sqrtl(__x); }
441 template<typename _Tp>
442 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
445 { return __builtin_sqrt(__x); }
451 { return __builtin_tanf(__x); }
455 { return __builtin_tanl(__x); }
457 template<typename _Tp>
458 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
461 { return __builtin_tan(__x); }
467 { return __builtin_tanhf(__x); }
470 tanh(long double __x)
471 { return __builtin_tanhl(__x); }
473 template<typename _Tp>
474 inline typename __gnu_cxx::__enable_if<__is_integer<_Tp>::__value,
477 { return __builtin_tanh(__x); }
479 _GLIBCXX_END_NAMESPACE
481 #if _GLIBCXX_USE_C99_MATH
482 #if !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC
484 // These are possible macros imported from C99-land.
492 #undef isgreaterequal
498 _GLIBCXX_BEGIN_NAMESPACE(std)
500 template<typename _Tp>
501 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
505 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
506 return __builtin_fpclassify(FP_NAN, FP_INFINITE, FP_NORMAL,
507 FP_SUBNORMAL, FP_ZERO, __type(__f));
510 template<typename _Tp>
511 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
515 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
516 return __builtin_isfinite(__type(__f));
519 template<typename _Tp>
520 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
524 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
525 return __builtin_isinf(__type(__f));
528 template<typename _Tp>
529 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
533 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
534 return __builtin_isnan(__type(__f));
537 template<typename _Tp>
538 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
542 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
543 return __builtin_isnormal(__type(__f));
546 template<typename _Tp>
547 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
551 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
552 return __builtin_signbit(__type(__f));
555 template<typename _Tp>
556 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
558 isgreater(_Tp __f1, _Tp __f2)
560 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
561 return __builtin_isgreater(__type(__f1), __type(__f2));
564 template<typename _Tp>
565 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
567 isgreaterequal(_Tp __f1, _Tp __f2)
569 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
570 return __builtin_isgreaterequal(__type(__f1), __type(__f2));
573 template<typename _Tp>
574 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
576 isless(_Tp __f1, _Tp __f2)
578 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
579 return __builtin_isless(__type(__f1), __type(__f2));
582 template<typename _Tp>
583 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
585 islessequal(_Tp __f1, _Tp __f2)
587 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
588 return __builtin_islessequal(__type(__f1), __type(__f2));
591 template<typename _Tp>
592 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
594 islessgreater(_Tp __f1, _Tp __f2)
596 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
597 return __builtin_islessgreater(__type(__f1), __type(__f2));
600 template<typename _Tp>
601 inline typename __gnu_cxx::__enable_if<__is_arithmetic<_Tp>::__value,
603 isunordered(_Tp __f1, _Tp __f2)
605 typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
606 return __builtin_isunordered(__type(__f1), __type(__f2));
609 _GLIBCXX_END_NAMESPACE
611 #endif /* _GLIBCXX_USE_C99_FP_MACROS_DYNAMIC */
614 #ifndef _GLIBCXX_EXPORT_TEMPLATE
615 # include <bits/cmath.tcc>
618 #ifdef __GXX_EXPERIMENTAL_CXX0X__
619 # if defined(_GLIBCXX_INCLUDE_AS_TR1)
620 # error C++0x header cannot be included from TR1 header
622 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
623 # include <tr1_impl/cmath>
625 # define _GLIBCXX_INCLUDE_AS_CXX0X
626 # define _GLIBCXX_BEGIN_NAMESPACE_TR1
627 # define _GLIBCXX_END_NAMESPACE_TR1
628 # define _GLIBCXX_TR1
629 # include <tr1_impl/cmath>
631 # undef _GLIBCXX_END_NAMESPACE_TR1
632 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
633 # undef _GLIBCXX_INCLUDE_AS_CXX0X